package Dao;


import entity.MemberBean;
import util.JDBCUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MemberDao {
    /**
     * 查询所有会员信息
     * */
    public List<MemberBean> getMemberAll(){
        List<MemberBean> list = null;
        String sql = "SELECT member_id,member_phone,balance,consume,member_state FROM Member_Information where member_id > 1";
        list = JDBCUtil.list(sql, MemberBean.class);
        return list;
    }
    /**
     * 电话号码查询信息
     * */
    public MemberBean getMemberByPhone(String member_phone){
        MemberBean member = null;
        String sql = "select member_id,member_phone,balance,consume,member_state from Member_Information where member_phone=?";
        member = JDBCUtil.one(sql, MemberBean.class,member_phone);
        return member;
    }
    /**
     * 通过id获取会员信息
     * */
    public MemberBean getMemberId(int id){
        MemberBean member = null;
        String sql = "select member_id,member_phone,balance,consume,member_state from Member_Information where member_id=?";
        member = JDBCUtil.one(sql, MemberBean.class,id);
        return member;
    }
    /**
     * 添加一个会员
     * */
    public int addMember(MemberBean member){
        int num = 0;
        String sql = "INSERT INTO Member_Information (member_phone, balance, consume,member_state) VALUES (?,?,?,?)";
        num = JDBCUtil.update(sql,member.getMember_phone(),member.getBalance(),member.getConsume(),"可用");
        return num;
    }
    /**
     * 修改会员信息
     * */
    public int updateMember(MemberBean member){
        int num = 0;
        String sql = "update Member_Information set member_phone=?,member_state=? where member_id =?";
        num = JDBCUtil.update(sql,member.getMember_phone(),member.getMember_state(),member.getMember_id());
        return num;
    }
    /**
     * 续费
     * */
    public int renew(int renews,int member_id){
        int num = 0;
        String sql = "update Member_Information set balance = balance + ? where member_id =?";
        num = JDBCUtil.update(sql,renews,member_id);
        return num;
    }
    /**
     * 通过id修改会员账号状态
     * */
    public int delMember(int member_id, String member_state){
        int num = 0;
        String sql = "update Member_Information set member_state =? where member_id =?";
        num = JDBCUtil.update(sql,member_state,member_id);
        return num;
    }

    public int conSumption(BigDecimal balance,BigDecimal consume,int member_id) {
        int num = 0;
        String sql = "update Member_Information set balance = ?,consume=? where member_id = ?";
        num = JDBCUtil.update(sql,balance,consume,member_id);
        return num;
    }

    public static void main(String[] args) {
//        BigDecimal balance = new BigDecimal("210.1");
//        BigDecimal consume = new BigDecimal("110.2");
//        MemberBean memberBean = new MemberBean(5,"111111", balance, consume);
//        System.out.println(memberBean);
//        int i = new MemberDao().updateMember(memberBean);
//        System.out.println(i);
        MemberBean memberByPhone = new MemberDao().getMemberByPhone("游客");
        System.out.println(memberByPhone);
    }
}
